<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>运算符优先级</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.html">运算符</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.arithmetic.html">算术运算符</a></div>
 <div class="up"><a href="language.operators.html">运算符</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.operators.precedence" class="sect1">
   <h2 class="title">运算符优先级</h2>
   <p class="para">
    运算符优先级指定了两个表达式绑定得有多“紧密”。例如，表达式 <em>1 + 5 *
    3</em> 的结果是 <em>16</em> 而不是 <em>18</em>
    是因为乘号（“*”）的优先级比加号（“+”）高。必要时可以用括号来强制改变优先级。例如：<em>(1
    + 5) * 3</em> 的值为 <em>18</em>。
   </p>
   <p class="para">
	如果运算符优先级相同，其结合方向决定着应该从右向左求值，还是从左向右求值——见下例。
   </p>
   <p class="para">
    下表按照优先级从高到低列出了运算符。同一行中的运算符具有相同优先级，此时它们的结合方向决定求值顺序。
    <table class="doctable table">
     <caption><strong>运算符优先级</strong></caption>
     
      <thead>
       <tr>
        <th>结合方向</th>
        <th>运算符</th>
        <th>附加信息</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>无</td>
        <td>clone new</td>
        <td><a href="language.oop5.cloning.html" class="link">clone</a> 和 <a href="language.oop5.basic.html#language.oop5.basic.new" class="link">new</a></td>
       </tr>

       <tr>
        <td>左</td>
        <td>[</td>
        <td><span class="function"><a href="function.array.html" class="function">array()</a></span></td>
       </tr>

       <tr>
        <td>右</td>
        <td>++ -- ~ (int) (float) (string) (array) (object) (bool) @</td>
        <td>
         <a href="language.types.html" class="link">类型</a>和<a href="language.operators.increment.html" class="link">递增／递减</a>
        </td>
       </tr>

       <tr>
        <td>无</td>
        <td>instanceof</td>
        <td>
         <a href="language.types.html" class="link">类型</a>
        </td>
       </tr>

       <tr>
        <td>右</td>
        <td>!</td>
        <td>
         <a href="language.operators.logical.html" class="link">逻辑运算符</a>
        </td>
       </tr>
      
       <tr>
        <td>左</td>
        <td>* / %</td>
        <td>
         <a href="language.operators.arithmetic.html" class="link">算术运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>+ - .</td>
        <td>
         <a href="language.operators.arithmetic.html" class="link">算术运算符</a>和<a href="language.operators.string.html" class="link">字符串运算符</a></td>
       </tr>

       <tr>
        <td>左</td>
        <td>&lt;&lt; &gt;&gt;</td>
        <td>
         <a href="language.operators.bitwise.html" class="link">位运算符</a>
        </td>
       </tr>

       <tr>
        <td>无</td>
        <td>== != === !== &lt;&gt;</td>
        <td>
         <a href="language.operators.comparison.html" class="link">比较运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>&amp;</td>
        <td>
         <a href="language.operators.bitwise.html" class="link">位运算符</a>和<a href="language.references.html" class="link">引用</a></td>
       </tr>

       <tr>
        <td>左</td>
        <td>^</td>
        <td>
         <a href="language.operators.bitwise.html" class="link">位运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>|</td>
        <td>
         <a href="language.operators.bitwise.html" class="link">位运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>&amp;&amp;</td>
        <td>
         <a href="language.operators.logical.html" class="link">逻辑运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>||</td>
        <td>
         <a href="language.operators.logical.html" class="link">逻辑运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>? :</td>
        <td>
         <a href="language.operators.comparison.html#language.operators.comparison.ternary" class="link">三元运算符</a>
        </td>
       </tr>

       <tr>
        <td>右</td>
        <td>
         = += -= *= /= .= %= &amp;= |= ^= &lt;&lt;= &gt;&gt;= =&gt;
        </td>
        <td>
         <a href="language.operators.assignment.html" class="link">赋值运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>and</td>
        <td>
         <a href="language.operators.logical.html" class="link">逻辑运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>xor</td>
        <td>
         <a href="language.operators.logical.html" class="link">逻辑运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>or</td>
        <td>
         <a href="language.operators.logical.html" class="link">逻辑运算符</a>
        </td>
       </tr>

       <tr>
        <td>左</td>
        <td>,</td>
        <td>多处用到</td>
       </tr>

      </tbody>
     
    </table>

   </p>
   <p class="para">
    对具有相同优先级的运算符，左结合方向意味着将从左向右求值，右结合方向则反之。对于无结合方向具有相同优先级的运算符，该运算符有可能无法与其自身结合。举例说，在 PHP
	中 <em>1 &lt; 2 &gt; 1</em> 是一个非法语句，而 <em>1 &lt;= 1 == 1</em>
	则不是。因为 <strong><code>T_IS_EQUAL</code></strong> 运算符的优先级比
    <strong><code>T_IS_SMALLER_OR_EQUAL</code></strong> 的运算符要低。
    <div class="example" id="example-114">
     <p><strong>Example #1 结合方向</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">%&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;(3&nbsp;*&nbsp;3)&nbsp;%&nbsp;5&nbsp;=&nbsp;4<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;(true&nbsp;?&nbsp;0&nbsp;:&nbsp;true)&nbsp;?&nbsp;1&nbsp;:&nbsp;2&nbsp;=&nbsp;2<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;=&nbsp;($b&nbsp;+=&nbsp;3)&nbsp;-&gt;&nbsp;$a&nbsp;=&nbsp;5,&nbsp;$b&nbsp;=&nbsp;5<br /><br />//&nbsp;mixing&nbsp;++&nbsp;and&nbsp;+&nbsp;produces&nbsp;undefined&nbsp;behavior<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />echo&nbsp;++</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">++;&nbsp;</span><span style="color: #FF8000">//&nbsp;may&nbsp;print&nbsp;4&nbsp;or&nbsp;5<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
    使用括号，即使在并不严格需要时，通常都可以增强代码的可读性。
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     尽管 <em>=</em> 比其它大多数的运算符的优先级低，PHP
     仍旧允许类似如下的表达式：<em>if (!$a = foo())</em>，在此例中
     <em>foo()</em> 的返回值被赋给了 <var class="varname"><var class="varname">$a</var></var>。
    </p>
   </p></blockquote>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.html">运算符</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.arithmetic.html">算术运算符</a></div>
 <div class="up"><a href="language.operators.html">运算符</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
